package com.gakj.truck.third.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gakj.truck.third.api.ouman.OumanService;
import com.gakj.truck.third.api.service.HuaTongService;
import com.gakj.truck.third.api.service.TruckOumanService;
import com.gakj.truck.third.core.domain.TruckOuman;
import com.gakj.truck.third.core.utils.CheckUtils;
import com.gakj.truck.third.core.utils.MediaTypes;
import com.gakj.truck.third.core.utils.RequestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * 第三方车辆相关操作
 * Created by gc on 2016/5/16.
 */
@RestController
@RequestMapping(value = "/third/truck")
public class TruckOumanController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TruckOumanController.class);

    @Autowired
    TruckOumanService truckOumanService;

    @Autowired
    OumanService oumanService;

    /**
     * 保存或者更新
     *
     * @param jsonString
     * @param source
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public JSONObject update(
            @RequestParam(value = "data", defaultValue = "") String jsonString,
            @RequestParam(value = "source", defaultValue = "0") String source
    ) throws Exception {

        LOGGER.info("车辆更新接口 [truckOumanController-update]");
        LOGGER.info("更新的信息:" + jsonString);
        if (StringUtils.isBlank(jsonString)) {
            return RequestUtils.failure("传递数据不能为空!");
        }
        return truckOumanService.saveOrUpdate(jsonString);
    }

    /**
     * 测试欧曼接口,是否正常返回
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONObject getTruckList() {
        return oumanService.getOumanTruckList(1);
    }

    /**
     * 保存数据到中间表
     *
     * @return
     */
    @RequestMapping(value = "init", method = RequestMethod.GET)
    public JSONObject saveToMediaTable() {
        try {
            truckOumanService.batchSave();
            return RequestUtils.success("");
        } catch (Exception e) {
            return RequestUtils.failure("保存失败! " + e.getMessage());
        }
    }

    /**
     * 将中间表中的数据保存到蜗牛数据库中
     *
     * @return
     */
    @RequestMapping(value = "/dealOld", method = RequestMethod.GET)
    public JSONObject dealOldData() {

        try {
            truckOumanService.dealOldData();
            return RequestUtils.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return RequestUtils.failure("处理历史数据出错!" + e.getMessage());
        }

    }


    @Autowired
    HuaTongService huaTongService;

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
    @ResponseBody
    public JSONObject saveTruck(
            @RequestParam(value = "id", defaultValue = "0") String id,
            @RequestParam(value = "imgList", defaultValue = "") String imgList,
            @RequestParam(value = "modelName", defaultValue = "") String modelName,
            @RequestParam(value = "seriesName", defaultValue = "") String seriesName,
            @RequestParam(value = "brandName", defaultValue = "") String brandName,
            @RequestParam(value = "price", defaultValue = "") Float price,
            @RequestParam(value = "showmileage", defaultValue = "") String showmileage,
            @RequestParam(value = "emission", defaultValue = "") String emission,
            @RequestParam(value = "power", defaultValue = "") String power,
            @RequestParam(value = "driveform", defaultValue = "") String driveform,
            @RequestParam(value = "firsthand", defaultValue = "1") String firsthand,
            @RequestParam(value = "color", defaultValue = "") String color,
            @RequestParam(value = "drivercard", defaultValue = "") String drivercard,
            @RequestParam(value = "contactname", defaultValue = "") String contactname,
            @RequestParam(value = "contacttel", defaultValue = "") String contacttel,
            @RequestParam(value = "seecarplace", defaultValue = "") String seecarplace,
            @RequestParam(value = "ownerdepict", defaultValue = "") String ownerdepict,
            @RequestParam(value = "createtime", defaultValue = "") String createtime,
            @RequestParam(value = "license", defaultValue = "") String licence,
            @RequestParam(value = "vin", defaultValue = "") String vin,
            @RequestParam(value = "cardtime", defaultValue = "") String cardtime,
            @RequestParam(value = "checktime", defaultValue = "") String checktime,
            @RequestParam(value = "source", defaultValue = "0") String source,
            @RequestParam(value = "title", defaultValue = "") String title,
            @RequestParam(value = "provinceName", defaultValue = "") String provinceName,
            @RequestParam(value = "cityName", defaultValue = "") String cityName,
            @RequestParam(value = "districtName", defaultValue = "") String districtName
    ) {


        if (id.equals("0") || StringUtils.isEmpty(id)) {
            return RequestUtils.failure("id不能位空且不能为0!");
        }
        if (StringUtils.isEmpty(imgList)) {
            return RequestUtils.failure("车辆图片传输错误");
        }
        if (StringUtils.isEmpty(modelName)) {
            return RequestUtils.failure("车型参数传递错误!");
        }
        if (StringUtils.isEmpty(seriesName)) {
            return RequestUtils.failure("车系参数错误");
        }
        if (StringUtils.isEmpty(brandName)) {
            return RequestUtils.failure("品牌参数错误!");
        }
        if (price == null || StringUtils.isEmpty(price.toString()) || price == 0) {
            return RequestUtils.failure("价格不能为空并且不能为0");
        }

        if (StringUtils.isEmpty(showmileage) || Integer.parseInt(showmileage) == 0) {
            return RequestUtils.failure("表显里程不能位空且不能为0!");
        }

        if (StringUtils.isEmpty(emission)) {
            return RequestUtils.failure("排放标准不能为空!");
        }
        if (StringUtils.isEmpty(power) || Integer.parseInt(power) == 0) {
            return RequestUtils.failure("马力不能为空且不能为0");
        }

        if (StringUtils.isEmpty(driveform)) {
            return RequestUtils.failure("驱动形式不能为空!");
        }

        if (StringUtils.isEmpty(color)) {
            return RequestUtils.failure("颜色不能为空!");
        }

        if (StringUtils.isEmpty(contactname)) {
            return RequestUtils.failure("联系人不能为空!");
        }

        if (StringUtils.isEmpty(contacttel)) {
            return RequestUtils.failure("联系电话不能为空!");
        }

        if (StringUtils.isEmpty(createtime)) {
            return RequestUtils.failure("createtime不能为空!");
        }

        if (StringUtils.isEmpty(vin)) {
            return RequestUtils.failure("VIN码不能位空!");
        }

        if (StringUtils.isEmpty(cardtime)) {
            return RequestUtils.failure("cardtime不能为空!");
        }

        if(StringUtils.isNotBlank(licence)&&!CheckUtils.checkLicence(licence)){
            return RequestUtils.failure("车牌号码格式不正确,如：鲁HN5675");
        }

        TruckOuman ouman = new TruckOuman();
        ouman.setImgList(imgList);
        ouman.setModelName(modelName);
        ouman.setSeriesName(seriesName);
        ouman.setBrandName(brandName);
        ouman.setPrice(price);
        ouman.setShowmileage(showmileage);
        ouman.setEmission(emission);
        ouman.setPower(power);
        ouman.setDriveform(driveform);
        ouman.setFirsthand(firsthand);
        ouman.setColor(color);
        ouman.setContacttel(contacttel);
        ouman.setContactname(contactname);
        ouman.setSeecarplace(seecarplace);
        ouman.setDrivercard(drivercard);
        ouman.setOwnerdepict(ownerdepict);
        ouman.setCreatetime(createtime);
        ouman.setLicence(licence);
        ouman.setVin(vin);
        ouman.setCardtime(cardtime);
        ouman.setChecktime(checktime);
        ouman.setSource(source);
        ouman.setTruckId(Long.valueOf(id));
        ouman.setTitle(title);
        ouman.setProvinceName(provinceName);
        ouman.setCityName(cityName);
        ouman.setAreaname(districtName);
        LOGGER.info("传输过来的请求参数:" + JSON.toJSONString(ouman));
        JSONObject result = huaTongService.saveOrUpdate(ouman);
        return result;
    }


    @RequestMapping(value = "/conserve",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject newSaveTruckInterface(
            @RequestParam(value = "jsonData",defaultValue = "") String jsonData
    ){
        JSONObject result = new JSONObject();
        ObjectMapper mapper = new ObjectMapper();
        String resMsg = "请求成功";
        if (StringUtils.isBlank(jsonData)) {
            resMsg = "数据传输错误";
            result.put("resMsg",resMsg);
            result.put("resCode","0");
            return result;
        }

        Map map = null;
        try {
            map = mapper.readValue(jsonData, Map.class);
        } catch (Exception e) {
            resMsg = "解析数据错误";
            result.put("resMsg",resMsg);
            result.put("resCode","0");
            return result;
        }
        result = huaTongService.newSaveOrUpdate(map);
        return result;
    }


    public static void main(String[] args) {
        String p2 = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
//        String pattern = "^[\\u4e00-\\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$";
        Pattern p = Pattern.compile(p2);

        String licence = "粤J12350";
        boolean matches = p.matcher(licence).matches();
        System.out.println(matches);
    }
}
